clear
capture cd "C:\Users\dal038902\Dropbox (GYU)\Study\2020 Spring\FDI_battle\JCR_Armed Conflict_Extractive FDI\replication\"
capture cd "C:\Users\dal038902\Dropbox\Study\2020 Spring\FDI_battle\JCR_Armed Conflict_Extractive FDI\replication\"
capture log using "00_analysis.log", replace


*(1) Parallel trends (Figure 2)
use data.dta, clear //49,776 obs.

twoway (lpolyci battle_300 time if foreign==0, lcolor(navy%60) ciplot(rline) blpattern(shortdash)) ///
(lpolyci battle_300 time if foreign==1, lcolor(cranberry%80) ciplot(rline) blpattern(shortdash)), ///
title("(a) Fractional Local Polynomial Estimates", size(large)) ytitle("Battles within 300 km" , size(large) margin(small)) ///
ylabel(0 (5) 15, labsize(medlarge) angle(zero) glwidth(vvvthin) glcolor(black%30) glpattern(shortdash)) ///
xtitle("") xline(0, lwidth(thin)) xlabel(-5 "T-5" -4 "T-4" -3 "T-3" -2 "T-2" -1 "T-1" 0 "T" 1 "T+1" 2 "T+2", labsize(medlarge) angle(zero)) ///
legend(col(2) pos(6) order(2 "Domestic Mines" 4 "Foreign-owned Mines") size(medlarge) region(lalign(center))) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) ///
lcolor(white) ifcolor(white) ilcolor(white)) name(g1, replace)
//Fractional Polynomial

capture drop time2
gen time2=time +5
reg battle_300 i.time2##i.foreign 

margins time2#foreign, plot(graphregion(fcolor(white)) plot1opts(msize(zero) lc(navy%60)) plot2opts(msize(zero) lcolor(cranberry%80)) recastci(rline) ci1opts(lpattern(shortdash) lc(navy%60)) ci2opts(lpattern(shortdash) lc(cranberry%60)) ///
 title("(b) Linear Regression Estimates with 8 periods", size(large)) ytitle("" , size(medium) margin(small)) ///
ylabel(, labsize(medsmall) angle(zero))  ///
xtitle("") xline(5, lwidth(thin)) xlabel(0 "T-5" 1 "T-4" 2 "T-3" 3 "T-2" 4 "T-1" 5 "T" 6 "T+1" 7 "T+2", labsize(medlarge) angle(zero)) ///
legend(col(2) pos(6) order(3 "Domestic Mines" 4 "Foreign-owned Mines")  size(medlarge) region(lalign(center))) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(g2, replace))
//Linear Regression

capture drop time3
gen time3 = 0
replace time3=1 if time==0
replace time3=2 if time>0
reg battle_300 i.time3##i.foreign 
margins time3#foreign, plot(graphregion(fcolor(white)) plot1opts(msize(zero) lc(navy%60)) plot2opts(msize(zero) lcolor(cranberry%80)) recastci(rline) ci1opts(lpattern(shortdash) lc(navy%60)) ci2opts(lpattern(shortdash) lc(cranberry%60)) ///
title("(c) Linear Regression Estimates with 3 periods", size(large)) ytitle("" , size(medium) margin(small)) ///
ylabel(, labsize(medsmall) angle(zero)) xline(1) xsize(20) ysize(10) ///
xtitle("") xline(5, lwidth(thin)) xlabel(-0.15 " " 0 "Pre-operation" 1 " " 2 "Post-operation" 2.15 " ", labsize(medlarge) angle(zero)) ///
legend(col(2) pos(6) order(3 "Domestic Mines" 4 "Foreign-owned Mines") size(medlarge) region(lalign(center))) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) ///
lcolor(white) ifcolor(white) ilcolor(white)) name(g3, replace)) 
//Linear regression after aggreagation

gr combine g1 g2 g3, xsize(20) ysize(6) scale(1) title("") graphregion(fcolor(white)) col(3)
graph export fig2.png, width(2000) as(png) replace




*(2) Balance Table (Table 1)

use data.dta, clear  
svyset [pw=w_battle_300_c]

capture program drop balance_table
program define balance_table, rclass
    syntax varname

    tempname M B V
    local var `varlist'

    * Survey means
    svy, subpop(if intervention==0): mean `var', over(foreign)
    matrix `M' = r(table)

    scalar mu0 = `M'[1,1]
    scalar mu1 = `M'[1,2]
    scalar se0 = `M'[2,1]
    scalar se1 = `M'[2,2]
    scalar n0  = 23510
    scalar n1  = 7600
	
	* Approximate SDs from SEs and sample size
    scalar sd0 = se0 * sqrt(n0)
    scalar sd1 = se1 * sqrt(n1)
	
	* SMD calculation
    scalar smd = abs(mu1 - mu0) / sqrt((sd1^2 + sd0^2)/2)

    * Survey regression
    svy, subpop(if intervention==0): regress `var' i.foreign
    test 1.foreign
    scalar diff = _b[1.foreign]
    scalar tval = _b[1.foreign] / _se[1.foreign]
    scalar pval = r(p)

    * Add stars
    local stars = cond(pval < 0.01, "***", cond(pval < 0.05, "**", cond(pval < 0.10, "*", "")))

    * Create row in matrix
    return scalar mu0 = mu0
    return scalar n0 = n0
    return scalar mu1 = mu1
    return scalar n1 = n1
    return scalar diff = diff
    return scalar tval = tval
	return scalar smd = smd
    return local stars = "`stars'"
end

//clear
tempname posth
postfile `posth' str15 variable double(mu0 n0 mu1 n1 diff smd tval) str3 stars using summary_table_temp, replace

foreach var in battle_300 lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3  log_aid reg_corruption f1 f2 f3 f4 f5 f6 f7 foreign_300 domestic_300 {
    quietly balance_table `var'
    post `posth' ("`var'") (r(mu0)) (r(n0)) (r(mu1)) (r(n1)) (r(diff)) (r(smd)) (r(tval)) ("`r(stars)'")
}
postclose `posth'

use summary_table_temp, clear

format mu0 mu1 diff %6.3f
format n0 n1 %6.0f
format tval %6.2f

list variable mu0 n0 mu1 n1 diff smd stars tval, sep(0) noobs


*(3) Main Analysis (Table 2)
use data.dta, clear //49,776 obs.

*Model 1 
local y battle_300
reg `y' i.foreign##i.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id if facility_type!=1, cluster(rec_id)
est sto m1_300

*Figure G2
est restore m1_300
margins, dydx(intervention) at(foreign=(0 1)) plot(recast(scatter) ///
ciopts(lpattern(dash)) ytitle("Marginal Effect of Mining Operation") ytitle(, size(medium) ///
margin(small)) yline(0) ylabel(, labsize(medsmall) angle(zero)) xtitle("") ///
xtitle(, size(medium)) xlabel(-0.1 " " 0 "Domestic-owned" 1 "Foreign-owned" 1.1  " ", labsize(medsmall)) ///
title("All Types") legend(on pos(6) order(1 "95% Confidence Interval" 2 "Battles within 300 km") rows(2)) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) ///
lcolor(white) ifcolor(white) ilcolor(white)) name(g1, replace))
graph export figG2.png, width(2000) as(png) replace

*Model 2
local y battle_300
reg `y' i.foreign##i.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m2_300

matrix out=r(table)'
matrix list out
scalar b_interv  = out["1.intervention", 1]
scalar se_interv = out["1.intervention", 2]

scalar b_interact  = out["1.foreign#1.intervention", 1]
scalar se_interact = out["1.foreign#1.intervention", 2]

matrix V = e(V)

scalar covar = V["1.intervention", "1.foreign#1.intervention"]

* marginal effect at foreign = 0
scalar me0 = b_interv
scalar se0 = se_interv
scalar ci_lo0 = me0 - 1.96 * se0
scalar ci_hi0 = me0 + 1.96 * se0

* marginal effect at foreign = 1
scalar me1 = b_interv + b_interact
scalar var1 = V["1.intervention", "1.intervention"] + V["1.foreign#1.intervention", "1.foreign#1.intervention"] + 2 * covar
scalar se1 = sqrt(var1)
scalar ci_lo1 = me1 - 1.96 * se1
scalar ci_hi1 = me1 + 1.96 * se1

capture drop lb0_m2_300_1
capture drop ub0_m2_300_1
capture drop me0_m2_300_1
capture drop lb1_m2_300_1
capture drop ub1_m2_300_1
capture drop me1_m2_300_1

matrix dydx_0=(me0)
matrix lb95_0=(ci_lo0)
matrix ub95_0=(ci_hi0)
matrix dydx_1=(me1)
matrix lb95_1=(ci_lo1)
matrix ub95_1=(ci_hi1)

svmat dydx_0, names(me0_m2_300_)
svmat lb95_0, names(lb0_m2_300_)
svmat ub95_0, names(ub0_m2_300_)

svmat dydx_1, names(me1_m2_300_)
svmat lb95_1, names(lb1_m2_300_)
svmat ub95_1, names(ub1_m2_300_)

local y battle_terr_300
reg `y' i.foreign##i.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_terr_300_c], cluster(rec_id)
est sto m2_terr_300

matrix out=r(table)'
matrix list out
scalar b_interv  = out["1.intervention", 1]
scalar se_interv = out["1.intervention", 2]

scalar b_interact  = out["1.foreign#1.intervention", 1]
scalar se_interact = out["1.foreign#1.intervention", 2]

matrix V = e(V)

scalar covar = V["1.intervention", "1.foreign#1.intervention"]

* marginal effect at foreign = 0
scalar me0 = b_interv
scalar se0 = se_interv
scalar ci_lo0 = me0 - 1.96 * se0
scalar ci_hi0 = me0 + 1.96 * se0

* marginal effect at foreign = 1
scalar me1 = b_interv + b_interact
scalar var1 = V["1.intervention", "1.intervention"] + V["1.foreign#1.intervention", "1.foreign#1.intervention"] + 2 * covar
scalar se1 = sqrt(var1)
scalar ci_lo1 = me1 - 1.96 * se1
scalar ci_hi1 = me1 + 1.96 * se1

capture drop lb0_m2_terr_300_1
capture drop ub0_m2_terr_300_1
capture drop me0_m2_terr_300_1
capture drop lb1_m2_terr_300_1
capture drop ub1_m2_terr_300_1
capture drop me1_m2_terr_300_1

matrix dydx_0=(me0)
matrix lb95_0=(ci_lo0)
matrix ub95_0=(ci_hi0)
matrix dydx_1=(me1)
matrix lb95_1=(ci_lo1)
matrix ub95_1=(ci_hi1)

svmat dydx_0, names(me0_m2_terr_300_)
svmat lb95_0, names(lb0_m2_terr_300_)
svmat ub95_0, names(ub0_m2_terr_300_)

svmat dydx_1, names(me1_m2_terr_300_)
svmat lb95_1, names(lb1_m2_terr_300_)
svmat ub95_1, names(ub1_m2_terr_300_)

local y battle_gov_300
reg `y' i.foreign##i.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_gov_300_c], cluster(rec_id)
est sto m2_gov_300

matrix out=r(table)'
matrix list out
scalar b_interv  = out["1.intervention", 1]
scalar se_interv = out["1.intervention", 2]

scalar b_interact  = out["1.foreign#1.intervention", 1]
scalar se_interact = out["1.foreign#1.intervention", 2]

matrix V = e(V)

scalar covar = V["1.intervention", "1.foreign#1.intervention"]

* marginal effect at foreign = 0
scalar me0 = b_interv
scalar se0 = se_interv
scalar ci_lo0 = me0 - 1.96 * se0
scalar ci_hi0 = me0 + 1.96 * se0

* marginal effect at foreign = 1
scalar me1 = b_interv + b_interact
scalar var1 = V["1.intervention", "1.intervention"] + V["1.foreign#1.intervention", "1.foreign#1.intervention"] + 2 * covar
scalar se1 = sqrt(var1)
scalar ci_lo1 = me1 - 1.96 * se1
scalar ci_hi1 = me1 + 1.96 * se1

capture drop lb0_m2_gov_300_1
capture drop ub0_m2_gov_300_1
capture drop me0_m2_gov_300_1
capture drop lb1_m2_gov_300_1
capture drop ub1_m2_gov_300_1
capture drop me1_m2_gov_300_1

matrix dydx_0=(me0)
matrix lb95_0=(ci_lo0)
matrix ub95_0=(ci_hi0)
matrix dydx_1=(me1)
matrix lb95_1=(ci_lo1)
matrix ub95_1=(ci_hi1)

svmat dydx_0, names(me0_m2_gov_300_)
svmat lb95_0, names(lb0_m2_gov_300_)
svmat ub95_0, names(ub0_m2_gov_300_)

svmat dydx_1, names(me1_m2_gov_300_)
svmat lb95_1, names(lb1_m2_gov_300_)
svmat ub95_1, names(ub1_m2_gov_300_)

est tab m1_300 m2_300 m2_terr_300 m2_gov_300,  ///
b(%9.3f) star(0.1 0.05 0.01) stats(N r2_a) drop(i.year i.admin_id) 

****************
****************

global models m1_300 m2_300 m2_terr_300 m2_gov_300

global coeflabel 1.foreign "Foreign_{i,t}" 1.intervention "T_{i,t}" 1.foreign#1.intervention "Foreign_{i,t} $\times$ T_{i,t}" lprice "Logged Commodity Price_{c,t-1}" pricechange "\Delta Commodity Price_{c,t-1}" mean "Average Ruggedness_i" min "Minimum Ruggedness_i" max "Maximum Ruggedness_i" std "Std. Dev. Ruggedness_i" past5 "Past Battles within 5 Years_{j,t}" polity2 "POLITY_{j,t-1}" gdppc "GDPPC_{j,t-1}" gdppc2 "GDPPC_{j,t-1}^2" gdppc3 "GDPPC_{j,t-1}^3" log_aid "Logged Aid_{j, t-1}" reg_corruption "Corruption_{j, t-1}" i.facility_type "Facility Type_{i}" foreign_300 "Foreign-owned Mines_{i,t}" domestic_300 "Domestic-owned Mines_{i,t}" csr_new "CSR Score_{i,t}"


local order 1.foreign 1.intervention 1.foreign#1.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption facility_type foreign_300 domestic_300 csr_new 

local mtitles "1" "2" "3" "4"

esttab $models using "tab2.tex",   ///
 b(%9.3f) se(%9.3f)  scalars(r2_a) sfmt(%9.2f) ///
 compress notes nogaps nolines replace star(* 0.1 ** 0.05 *** 0.01) nonumbers ///
 title("Regression Analysis" \label{tab:main}) ///
 mtitles(`mtitles') drop(*.year *.admin_id) label coeflabels($coeflabel)  ///
 nobaselevels  order(`order') /// 
 addnotes("Entries in parentheses are standard errors clustered at facility ($i$).")
 
****************
****************

*(4) The Effects of Foreign Ownership (Figure 3)

foreach n in m2_300 m2_terr_300 m2_gov_300 {
capture rename lb0_`n'_ lb0_`n'
capture rename ub0_`n'_ ub0_`n'
capture rename me0_`n'_ me0_`n'
capture rename lb1_`n'_ lb1_`n'
capture rename ub1_`n'_ ub1_`n'
capture rename me1_`n'_ me1_`n'
}


keep me0_* lb0_* ub0_* me1_* lb1_* ub1_*
gen id=_n
keep if id==1
reshape long me0_m2_ lb0_m2_ ub0_m2_ me1_m2_ lb1_m2_ ub1_m2_ me0_m2_terr_ lb0_m2_terr_ ub0_m2_terr_ me1_m2_terr_ lb1_m2_terr_ ub1_m2_terr_ me0_m2_gov_ lb0_m2_gov_ ub0_m2_gov_ me1_m2_gov_ lb1_m2_gov_ ub1_m2_gov_, i(id) j(d)
capture drop d0 
capture drop d1
gen d1 = 1
gen d0 = 0

*Correction (STATA systemically miscalculate the confidence intervals)

replace me0_m2_terr_=-.0934593
replace lb0_m2_terr_=-1.55764
replace ub0_m2_terr_=1.370721
replace me1_m2_terr_=-2.338668
replace lb1_m2_terr_=-3.597222
replace ub1_m2_terr_=-1.080115
			
graph twoway (rcap lb0_m2_ ub0_m2_ d0, lpattern(shortdash) lc(red%60)) ///
(connected me0_m2_ d0, mcolor(red%80) yline(0) msize(small) lpattern(solid) lc(red) ylabel(, gmax angle(horizontal)))  /// 
(rcap lb1_m2_ ub1_m2_ d1, lpattern(shortdash) lc(red%60)) ///
(connected me1_m2_ d1, mcolor(red%80) yline(0) msize(small) lpattern(solid) lc(red) ylabel(, gmax angle(horizontal))), /// 
ytitle("Marginal Effect of Mining Operation") ytitle(, size(medium) ///
margin(small)) yline(0) ylabel(, labsize(medsmall) angle(zero)) xtitle("") ///
xtitle(, size(medium)) xlabel(-0.2 " " 0 "Domestic-owned" 1 "Foreign-owned" 1.2  " ", labsize(medsmall)) ///
title("All Types") legend(on pos(6) order(1 "95% Confidence Interval" 2 "Battles within 300 km") rows(2)) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) ///
lcolor(white) ifcolor(white) ilcolor(white)) name(g1, replace)

graph twoway (rcap lb0_m2_terr ub0_m2_terr d0, lpattern(shortdash) lc(red%60)) ///
(connected me0_m2_terr d0, mcolor(red%80) yline(0) msize(small) lpattern(solid) lc(red) ylabel(, gmax angle(horizontal)))  /// 
(rcap lb1_m2_terr ub1_m2_terr d1, lpattern(shortdash) lc(red%60)) ///
(connected me1_m2_terr d1, mcolor(red%80) yline(0) msize(small) lpattern(solid) lc(red) ylabel(, gmax angle(horizontal))), /// 
ytitle("Marginal Effect of Mining Operation") ytitle(, size(medium) ///
margin(small)) yline(0) ylabel(, labsize(medsmall) angle(zero)) xtitle("") ///
xtitle(, size(medium)) xlabel(-0.2 " " 0 "Domestic-owned" 1 "Foreign-owned" 1.2  " ", labsize(medsmall)) ///
title("Territorial Incompatibility") legend(on pos(6) order(1 "95% Confidence Interval" 2 "Battles within 300 km") rows(2)) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) ///
lcolor(white) ifcolor(white) ilcolor(white)) name(g2, replace)

graph twoway (rcap lb0_m2_gov ub0_m2_gov d0, lpattern(shortdash) lc(red%60)) ///
(connected me0_m2_gov d0, mcolor(red%80) yline(0) msize(small) lpattern(solid) lc(red) ylabel(, gmax angle(horizontal)))  /// 
(rcap lb1_m2_gov ub1_m2_gov d1, lpattern(shortdash) lc(red%60)) ///
(connected me1_m2_gov d1, mcolor(red%80) yline(0) msize(small) lpattern(solid) lc(red) ylabel(, gmax angle(horizontal))), /// 
ytitle("Marginal Effect of Mining Operation") ytitle(, size(medium) ///
margin(small)) yline(0) ylabel(, labsize(medsmall) angle(zero)) xtitle("") ///
xtitle(, size(medium)) xlabel(-0.2 " " 0 "Domestic-owned" 1 "Foreign-owned" 1.2  " ", labsize(medsmall)) ///
title("Government Incompatibility") legend(on pos(6) order(1 "95% Confidence Interval" 2 "Battles within 300 km") rows(2)) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) ///
lcolor(white) ifcolor(white) ilcolor(white)) name(g3, replace)

gr combine g1 g2 g3, col(3) xsize(20) ysize(10) scale(1) title("") graphregion(fcolor(white))
graph export fig3.png, width(2000) as(png) replace



*(5) The Effects of Foreign Ownership across Distance Bands and Battle-related Deaths (Table G1, G2, Figure 4)
*Battle Death data to overlay histogram
use data.dta, clear //49,776 obs.
joinby rec_id year using death.dta, unmatched(master) _merge(a)
tab a
drop a

collapse (mean) death_*
gen id=_n
reshape long death_, i(id) j(d)
drop id
rename death_ death
save mean_death.dta, replace

use data.dta, clear //49,776 obs.
joinby rec_id year using death.dta, unmatched(master) _merge(a)
tab a
drop a

collapse (mean) battle_*
drop battle_*_*
gen id=_n
reshape long battle_, i(id) j(d)
drop id
rename battle_ battle
save mean_battle.dta, replace

use data.dta, clear //49,776 obs.
joinby rec_id year using death.dta, unmatched(master) _merge(a)
tab a
drop a

collapse (mean) battle_*_*
drop battle_terr* battle_gov*
gen id=_n
reshape long battle_500_ battle_450_ battle_400_ battle_350_ battle_300_ battle_250_ battle_200_ battle_150_ battle_100_ battle_50_ battle_30_ battle_10_, i(id) j(d_cat)
drop id
gen id=_n
foreach n in 500 450 400 350 300 250 200 150 100 50 30 10 {
capture rename battle_`n'_ battle_`n'
}
reshape long battle_, i(id) j(d)
drop id
reshape wide battle_, i(d) j(d_cat)
save mean_battle_d_cat.dta, replace

joinby d using mean_battle, unmatched(both) _merge(a)
drop a
joinby d using mean_death, unmatched(both) _merge(a)
drop a
save battle_death.dta, replace

*Running models
use data.dta, clear //49,776 obs.

foreach n in 500 450 400 350 300 250 200 150 100 50 30 10 {
local y battle_`n'
reg `y' i.foreign##i.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_`n' domestic_`n' csr_new i.year i.admin_id [pw=w_battle_`n'_a], cluster(rec_id)
est sto m2_`n'

matrix out=r(table)'
matrix list out
scalar b_interv  = out["1.intervention", 1]
scalar se_interv = out["1.intervention", 2]

scalar b_interact  = out["1.foreign#1.intervention", 1]
scalar se_interact = out["1.foreign#1.intervention", 2]

matrix V = e(V)

scalar covar = V["1.intervention", "1.foreign#1.intervention"]

* marginal effect at foreign = 0
scalar me0 = b_interv
scalar se0 = se_interv
scalar ci_lo0 = me0 - 1.96 * se0
scalar ci_hi0 = me0 + 1.96 * se0

* marginal effect at foreign = 1
scalar me1 = b_interv + b_interact
scalar var1 = V["1.intervention", "1.intervention"] + V["1.foreign#1.intervention", "1.foreign#1.intervention"] + 2 * covar
scalar se1 = sqrt(var1)
scalar ci_lo1 = me1 - 1.96 * se1
scalar ci_hi1 = me1 + 1.96 * se1

capture drop lb0_`n'_1
capture drop ub0_`n'_1
capture drop me0_`n'_1
capture drop lb1_`n'_1
capture drop ub1_`n'_1
capture drop me1_`n'_1

matrix dydx_0=(me0)
matrix lb95_0=(ci_lo0)
matrix ub95_0=(ci_hi0)
matrix dydx_1=(me1)
matrix lb95_1=(ci_lo1)
matrix ub95_1=(ci_hi1)

svmat dydx_0, names(me0_`n'_)
svmat lb95_0, names(lb0_`n'_)
svmat ub95_0, names(ub0_`n'_)

svmat dydx_1, names(me1_`n'_)
svmat lb95_1, names(lb1_`n'_)
svmat ub95_1, names(ub1_`n'_)

}

est tab m2_500 m2_450 m2_400 m2_350 m2_300, ///
b(%9.3f) star(0.1 0.05 0.01) stats(N r2_a) drop(i.year i.admin_id) 

est tab m2_250 m2_200 m2_150 m2_100 m2_50 m2_30 m2_10,  ///
b(%9.3f) star(0.1 0.05 0.01) stats(N r2_a) drop(i.year i.admin_id) 

****************
****************
*(6-1) The Effects of Foreign Ownership on Battle across Distance Bands (Table G1)

global models m2_500 m2_450 m2_400 m2_350 m2_300 m2_250 m2_200 m2_150 m2_100 m2_50 m2_30 m2_10

global coeflabel 1.foreign "Foreign_{i,t}" 1.intervention "T_{i,t}" 1.foreign#1.intervention "Foreign_{i,t} $\times$ T_{i,t}" lprice "Logged Commodity Price_{c,t-1}" pricechange "\Delta Commodity Price_{c,t-1}" mean "Average Ruggedness_i" min "Minimum Ruggedness_i" max "Maximum Ruggedness_i" std "Std. Dev. Ruggedness_i" past5 "Past Battles within 5 Years_{j,t}" polity2 "POLITY_{j,t-1}" gdppc "GDPPC_{j,t-1}" gdppc2 "GDPPC_{j,t-1}^2" gdppc3 "GDPPC_{j,t-1}^3" log_aid "Logged Aid_{j, t-1}" reg_corruption "Corruption_{j, t-1}" i.facility_type "Facility Type_{i}" csr_new "CSR Score_{i,t}"


local order 1.foreign 1.intervention 1.foreign#1.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption facility_type foreign_* domestic_* csr_new 

local mtitles "500" "450" "400" "350" "300" "250" "200" "150" "100" "50" "30" "10"

esttab $models using "tab_g1.tex",   ///
 b(%9.3f) se(%9.3f)  scalars(r2_a) sfmt(%9.2f) ///
 compress notes nogaps nolines replace star(* 0.1 ** 0.05 *** 0.01) nonumbers ///
 title("Regression Analysis" \label{tab:main}) ///
 mtitles(`mtitles') drop(*.year *.admin_id) label coeflabels($coeflabel)  ///
 nobaselevels  order(`order') /// 
 addnotes("Entries in parentheses are standard errors clustered at facility ($i$).")
 
****************
****************

*(6-2) The Effects of Foreign Ownership on Battle across Distance Bands (Left Plot in Figure 2)

foreach n in 500 450 400 350 300 250 200 150 100 50 30 10 {
capture rename lb0_`n'_1 lb0_`n'
capture rename ub0_`n'_1 ub0_`n'
capture rename me0_`n'_1 me0_`n'
capture rename lb1_`n'_1 lb1_`n'
capture rename ub1_`n'_1 ub1_`n'
capture rename me1_`n'_1 me1_`n'
}

keep me0_* lb0_* ub0_* me1_* lb1_* ub1_*
gen id=_n
keep if id==1
reshape long me0_ lb0_ ub0_ me1_ lb1_ ub1_, i(id) j(d)

rename ub0_ ub0
rename lb0_ lb0
rename me0_ me0
rename ub1_ ub1
rename lb1_ lb1
rename me1_ me1
replace id = _n
reshape long me lb ub, i(id) j(foreign)

joinby d using battle_death.dta, unmatched(both) _merge(a)
drop a

*Correction
//me1_450_1	lb1_450_1	ub1_450_1
//-1.66794	-3.371107	.0352278

replace lb = -3.371107 if d==450&foreign==1
replace ub = .0352278 if d==450&foreign==1

graph twoway (rcap lb ub d if foreign==1, lpattern(shortdash) lc(red%60)) ///
(connected me d if foreign==1, mcolor(red%30) yline(0) msize(small) lpattern(solid) lc(red) ylabel(, gmax angle(horizontal))) ///
(bar battle d, lw(vthin) barwidth(10) fcolor(none) lc(gray) yaxis(2)), /// 
xtitle("Distance from Mining Facilities (km)") xlabel(, labsize(medsmall) angle(20)) graphregion(fcolor(white)) ytitle("No. of Battles", axis(2)) ///
ytitle("Marginal Effect of Mining Operation", size(medium) margin(tiny)) ///
legend(on pos(6) col(3) order(3 "Average No. of Battles" )) xsize(15) ysize(10) name(g_dist_all, replace)

*(6-3) The Effects of Foreign Ownership on Battle-related Deaths across Distance Bands (Table G2)
use data.dta, clear //49,776 obs.
joinby rec_id year using death.dta, unmatched(master) _merge(a)
tab a
drop a


foreach n in 500 450 400 350 300 250 200 150 100 50 30 10 {
replace death_`n'=0 if death_`n'==.
local y death_`n'
reg `y' i.foreign##i.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_`n' domestic_`n' csr_new i.year i.admin_id [pw=w_battle_`n'_a], cluster(rec_id)
est sto m3_`n'


matrix out=r(table)'
matrix list out
scalar b_interv  = out["1.intervention", 1]
scalar se_interv = out["1.intervention", 2]

scalar b_interact  = out["1.foreign#1.intervention", 1]
scalar se_interact = out["1.foreign#1.intervention", 2]

matrix V = e(V)

scalar covar = V["1.intervention", "1.foreign#1.intervention"]

* marginal effect at foreign = 0
scalar me0 = b_interv
scalar se0 = se_interv
scalar ci_lo0 = me0 - 1.96 * se0
scalar ci_hi0 = me0 + 1.96 * se0

* marginal effect at foreign = 1
scalar me1 = b_interv + b_interact
scalar var1 = V["1.intervention", "1.intervention"] + V["1.foreign#1.intervention", "1.foreign#1.intervention"] + 2 * covar
scalar se1 = sqrt(var1)
scalar ci_lo1 = me1 - 1.96 * se1
scalar ci_hi1 = me1 + 1.96 * se1

capture drop lb0_`n'_`i'1
capture drop ub0_`n'_`i'1
capture drop me0_`n'_`i'1
capture drop lb1_`n'_`i'1
capture drop ub1_`n'_`i'1
capture drop me1_`n'_`i'1

matrix dydx_0=(me0)
matrix lb95_0=(ci_lo0)
matrix ub95_0=(ci_hi0)
matrix dydx_1=(me1)
matrix lb95_1=(ci_lo1)
matrix ub95_1=(ci_hi1)

svmat dydx_0, names(me0_`n'_`i')
svmat lb95_0, names(lb0_`n'_`i')
svmat ub95_0, names(ub0_`n'_`i')

svmat dydx_1, names(me1_`n'_`i')
svmat lb95_1, names(lb1_`n'_`i')
svmat ub95_1, names(ub1_`n'_`i')

}

est tab m3_500 m3_450 m3_400 m3_350 m3_300, ///
b(%9.5f) star(0.1 0.05 0.01) stats(N r2_a) drop(i.year i.admin_id) 

est tab m3_250 m3_200 m3_150 m3_100 m3_50 m3_30 m3_10,  ///
b(%9.5f) star(0.1 0.05 0.01) stats(N r2_a) drop(i.year i.admin_id) 

********
***************

global models m3_500 m3_450 m3_400 m3_350 m3_300 m3_250 m3_200 m3_150 m3_100 m3_50 m3_30 m3_10

global coeflabel 1.foreign "Foreign_{i,t}" 1.intervention "T_{i,t}" 1.foreign#1.intervention "Foreign_{i,t} $\times$ T_{i,t}" lprice "Logged Commodity Price_{c,t-1}" pricechange "\Delta Commodity Price_{c,t-1}" mean "Average Ruggedness_i" min "Minimum Ruggedness_i" max "Maximum Ruggedness_i" std "Std. Dev. Ruggedness_i" past5 "Past Battles within 5 Years_{j,t}" polity2 "POLITY_{j,t-1}" gdppc "GDPPC_{j,t-1}" gdppc2 "GDPPC_{j,t-1}^2" gdppc3 "GDPPC_{j,t-1}^3" log_aid "Logged Aid_{j, t-1}" reg_corruption "Corruption_{j, t-1}" i.facility_type "Facility Type_{i}" csr_new "CSR Score_{i,t}"


local order 1.foreign 1.intervention 1.foreign#1.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption facility_type foreign_* domestic_* csr_new 

local mtitles "500" "450" "400" "350" "300" "250" "200" "150" "100" "50" "30" "10"

esttab $models using "tab_g2.tex",   ///
 b(%9.3f) se(%9.3f)  scalars(r2_a) sfmt(%9.2f) ///
 compress notes nogaps nolines replace star(* 0.1 ** 0.05 *** 0.01) nonumbers ///
 title("Regression Analysis" \label{tab:main}) ///
 mtitles(`mtitles') drop(*.year *.admin_id) label coeflabels($coeflabel)  ///
 nobaselevels  order(`order') /// 
 addnotes("Entries in parentheses are standard errors clustered at facility ($i$).")
 
 ***************
 ***************
*(6-4) The Effects of Foreign Ownership on Battle-related Deaths across Distance Bands (Figure 4)

foreach n in 500 450 400 350 300 250 200 150 100 50 30 10 {
capture rename lb0_`n'_`i'1 lb0_`n'
capture rename ub0_`n'_`i'1 ub0_`n'
capture rename me0_`n'_`i'1 me0_`n'
capture rename lb1_`n'_`i'1 lb1_`n'
capture rename ub1_`n'_`i'1 ub1_`n'
capture rename me1_`n'_`i'1 me1_`n'
}


keep me0_* lb0_* ub0_* me1_* lb1_* ub1_*
gen id=_n
keep if id==1
reshape long me0_ lb0_ ub0_ me1_ lb1_ ub1_, i(id) j(d)

rename ub0_ ub0
rename lb0_ lb0
rename me0_ me0
rename ub1_ ub1
rename lb1_ lb1
rename me1_ me1
replace id = _n
reshape long me lb ub, i(id) j(foreign)

joinby d using battle_death.dta, unmatched(both) _merge(a)
drop a

*Correction
//me1_450_1	lb1_450_1	ub1_450_1
//-35.52745	-51.38342	-19.67148


replace lb = -51.38342 if d==450&foreign==1
replace ub = -19.67148 if d==450&foreign==1

*Right Plot in Figure 4
graph twoway (rcap lb ub d if foreign==1, lpattern(shortdash) lc(red%60)) ///
(connected me d if foreign==1, mcolor(red%30) yline(0) msize(small) lpattern(solid) lc(red) ylabel(, gmax angle(horizontal))) ///
(bar death d, lw(vthin) fcolor(none) lc(gray) barwidth(10) yaxis(2)), /// 
xtitle("Distance from Mining Facilities (km)") xlabel(, labsize(medsmall) angle(20)) graphregion(fcolor(white)) ytitle("No. of Deaths", axis(2)) ///
ytitle("Marginal Effect of Mining Operation", size(medium) margin(tiny)) ///
legend(on pos(6) col(3) order(3 "Average No. of Deaths" )) xsize(15) ysize(10) name(g_death_all, replace)

graph combine g_dist_all g_death_all, xsize(20) ysize(10) scale(1) title("") graphregion(fcolor(white))
graph export fig4.png, width(2000) as(png) replace


***************************
***************************


*(7) Marginal Effect across different casualties level (Table G3-6)
*(7-1) death<25
use data.dta, clear //49,776 obs.

foreach n in 500 450 400 350 300 250 200 150 100 50 30 10 {
foreach i in 0 {
local y battle_`n'_`i'
reg `y' i.foreign##i.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_`n' domestic_`n' csr_new i.year i.admin_id [pw=w_battle_`n'_`i'_a], cluster(rec_id)
est sto m2_`n'_`i' 


matrix out=r(table)'
matrix list out
scalar b_interv  = out["1.intervention", 1]
scalar se_interv = out["1.intervention", 2]

scalar b_interact  = out["1.foreign#1.intervention", 1]
scalar se_interact = out["1.foreign#1.intervention", 2]

matrix V = e(V)

scalar covar = V["1.intervention", "1.foreign#1.intervention"]

* marginal effect at foreign = 0
scalar me0 = b_interv
scalar se0 = se_interv
scalar ci_lo0 = me0 - 1.96 * se0
scalar ci_hi0 = me0 + 1.96 * se0

* marginal effect at foreign = 1
scalar me1 = b_interv + b_interact
scalar var1 = V["1.intervention", "1.intervention"] + V["1.foreign#1.intervention", "1.foreign#1.intervention"] + 2 * covar
scalar se1 = sqrt(var1)
scalar ci_lo1 = me1 - 1.96 * se1
scalar ci_hi1 = me1 + 1.96 * se1

capture drop lb0_`n'_`i'1
capture drop ub0_`n'_`i'1
capture drop me0_`n'_`i'1
capture drop lb1_`n'_`i'1
capture drop ub1_`n'_`i'1
capture drop me1_`n'_`i'1

matrix dydx_0=(me0)
matrix lb95_0=(ci_lo0)
matrix ub95_0=(ci_hi0)
matrix dydx_1=(me1)
matrix lb95_1=(ci_lo1)
matrix ub95_1=(ci_hi1)

svmat dydx_0, names(me0_`n'_`i')
svmat lb95_0, names(lb0_`n'_`i')
svmat ub95_0, names(ub0_`n'_`i')

svmat dydx_1, names(me1_`n'_`i')
svmat lb95_1, names(lb1_`n'_`i')
svmat ub95_1, names(ub1_`n'_`i')

}
}

est tab m2_500_0 m2_450_0 m2_400_0 m2_350_0 m2_300_0, ///
b(%9.3f) star(0.1 0.05 0.01) stats(N r2_a) drop(i.year i.admin_id) 

est tab m2_250_0 m2_200_0 m2_150_0 m2_100_0 m2_50_0 m2_30_0 m2_10_0,  ///
b(%9.3f) star(0.1 0.05 0.01) stats(N r2_a) drop(i.year i.admin_id) 

***************
***************

global models m2_500_0 m2_450_0 m2_400_0 m2_350_0 m2_300_0 m2_250_0 m2_200_0 m2_150_0 m2_100_0 m2_50_0 m2_30_0 m2_10

global coeflabel 1.foreign "Foreign_{i,t}" 1.intervention "T_{i,t}" 1.foreign#1.intervention "Foreign_{i,t} $\times$ T_{i,t}" lprice "Logged Commodity Price_{c,t-1}" pricechange "\Delta Commodity Price_{c,t-1}" mean "Average Ruggedness_i" min "Minimum Ruggedness_i" max "Maximum Ruggedness_i" std "Std. Dev. Ruggedness_i" past5 "Past Battles within 5 Years_{j,t}" polity2 "POLITY_{j,t-1}" gdppc "GDPPC_{j,t-1}" gdppc2 "GDPPC_{j,t-1}^2" gdppc3 "GDPPC_{j,t-1}^3" log_aid "Logged Aid_{j, t-1}" reg_corruption "Corruption_{j, t-1}" i.facility_type "Facility Type_{i}" csr_new "CSR Score_{i,t}"


local order 1.foreign 1.intervention 1.foreign#1.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption facility_type foreign_* domestic_* csr_new 

local mtitles "500" "450" "400" "350" "300" "250" "200" "150" "100" "50" "30" "10"

esttab $models using "tab_g3.tex",   ///
 b(%9.3f) se(%9.3f)  scalars(r2_a) sfmt(%9.2f) ///
 compress notes nogaps nolines replace star(* 0.1 ** 0.05 *** 0.01) nonumbers ///
 title("Regression Analysis" \label{tab:main}) ///
 mtitles(`mtitles') drop(*.year *.admin_id) label coeflabels($coeflabel)  ///
 nobaselevels  order(`order') /// 
 addnotes("Entries in parentheses are standard errors clustered at facility ($i$).")
 
 ***************
 ***************
 
foreach n in 500 450 400 350 300 250 200 150 100 50 30 10 {
foreach i in 0 {
capture rename lb0_`n'_`i'1 lb0_`n'
capture rename ub0_`n'_`i'1 ub0_`n'
capture rename me0_`n'_`i'1 me0_`n'
capture rename lb1_`n'_`i'1 lb1_`n'
capture rename ub1_`n'_`i'1 ub1_`n'
capture rename me1_`n'_`i'1 me1_`n'
}
}

keep me0_* lb0_* ub0_* me1_* lb1_* ub1_*
gen id=_n
keep if id==1
reshape long me0_ lb0_ ub0_ me1_ lb1_ ub1_, i(id) j(d)


rename ub0_ ub0
rename lb0_ lb0
rename me0_ me0
rename ub1_ ub1
rename lb1_ lb1
rename me1_ me1
replace id = _n
reshape long me lb ub, i(id) j(foreign)

joinby d using battle_death.dta, unmatched(both) _merge(a)
drop a

*Correction
//me1_450_01	lb1_450_01	ub1_450_01
//-1.373482	-3.026231	.2792678

replace lb = -3.026231 if d==450&foreign==1
replace ub = .2792678 if d==450&foreign==1

graph twoway (rcap lb ub d if foreign==1, lpattern(shortdash) lc(red%60)) ///
(connected me d if foreign==1, mcolor(red%30) yline(0) msize(small) lpattern(solid) lc(red) ylabel(, gmax angle(horizontal))) ///
(bar battle_0 d, lw(vthin) fcolor(none) lc(gray) barwidth(10) yaxis(2)), /// 
xtitle("Distance from Mining Facilities (km)") xlabel(, labsize(medsmall) angle(20)) graphregion(fcolor(white)) ytitle("Average No. of Battles", axis(2)) ///
ytitle(" ", size(medium) margin(tiny)) ///
legend(on pos(6) col(3) order(3 "Death<25" )) xsize(15) ysize(10) name(g1_0, replace)
 
 ***************
 ***************


*(7-2) 100>death>25
use data.dta, clear //49,776 obs.

foreach n in 500 450 400 350 300 250 200 150 100 50 30 10 {
foreach i in 1 {
local y battle_`n'_`i'
reg `y' i.foreign##i.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_`n' domestic_`n' csr_new i.year i.admin_id [pw=w_battle_`n'_`i'_a], cluster(rec_id)
est sto m2_`n'_`i' 

matrix out=r(table)'
matrix list out
scalar b_interv  = out["1.intervention", 1]
scalar se_interv = out["1.intervention", 2]

scalar b_interact  = out["1.foreign#1.intervention", 1]
scalar se_interact = out["1.foreign#1.intervention", 2]

matrix V = e(V)

scalar covar = V["1.intervention", "1.foreign#1.intervention"]

* marginal effect at foreign = 0
scalar me0 = b_interv
scalar se0 = se_interv
scalar ci_lo0 = me0 - 1.96 * se0
scalar ci_hi0 = me0 + 1.96 * se0

* marginal effect at foreign = 1
scalar me1 = b_interv + b_interact
scalar var1 = V["1.intervention", "1.intervention"] + V["1.foreign#1.intervention", "1.foreign#1.intervention"] + 2 * covar
scalar se1 = sqrt(var1)
scalar ci_lo1 = me1 - 1.96 * se1
scalar ci_hi1 = me1 + 1.96 * se1

capture drop lb0_`n'_`i'1
capture drop ub0_`n'_`i'1
capture drop me0_`n'_`i'1
capture drop lb1_`n'_`i'1
capture drop ub1_`n'_`i'1
capture drop me1_`n'_`i'1

matrix dydx_0=(me0)
matrix lb95_0=(ci_lo0)
matrix ub95_0=(ci_hi0)
matrix dydx_1=(me1)
matrix lb95_1=(ci_lo1)
matrix ub95_1=(ci_hi1)

svmat dydx_0, names(me0_`n'_`i')
svmat lb95_0, names(lb0_`n'_`i')
svmat ub95_0, names(ub0_`n'_`i')

svmat dydx_1, names(me1_`n'_`i')
svmat lb95_1, names(lb1_`n'_`i')
svmat ub95_1, names(ub1_`n'_`i')

}
}


est tab m2_500_1 m2_450_1 m2_400_1 m2_350_1 m2_300_1, ///
b(%9.3f) star(0.1 0.05 0.01) stats(N r2_a) drop(i.year i.admin_id) 

est tab m2_250_1 m2_200_1 m2_150_1 m2_100_1 m2_50_1 m2_30_1 m2_10_1,  ///
b(%9.3f) star(0.1 0.05 0.01) stats(N r2_a) drop(i.year i.admin_id) 

********
***************

global models m2_500_1 m2_450_1 m2_400_1 m2_350_1 m2_300_1 m2_250_1 m2_200_1 m2_150_1 m2_100_1 m2_50_1 m2_30_1 m2_10

global coeflabel 1.foreign "Foreign_{i,t}" 1.intervention "T_{i,t}" 1.foreign#1.intervention "Foreign_{i,t} $\times$ T_{i,t}" lprice "Logged Commodity Price_{c,t-1}" pricechange "\Delta Commodity Price_{c,t-1}" mean "Average Ruggedness_i" min "Minimum Ruggedness_i" max "Maximum Ruggedness_i" std "Std. Dev. Ruggedness_i" past5 "Past Battles within 5 Years_{j,t}" polity2 "POLITY_{j,t-1}" gdppc "GDPPC_{j,t-1}" gdppc2 "GDPPC_{j,t-1}^2" gdppc3 "GDPPC_{j,t-1}^3" log_aid "Logged Aid_{j, t-1}" reg_corruption "Corruption_{j, t-1}" i.facility_type "Facility Type_{i}" csr_new "CSR Score_{i,t}"


local order 1.foreign 1.intervention 1.foreign#1.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption facility_type foreign_* domestic_* csr_new 

local mtitles "500" "450" "400" "350" "300" "250" "200" "150" "100" "50" "30" "10"

esttab $models using "tab_g4.tex",   ///
 b(%9.3f) se(%9.3f)  scalars(r2_a) sfmt(%9.2f) ///
 compress notes nogaps nolines replace star(* 0.1 ** 0.05 *** 0.01) nonumbers ///
 title("Regression Analysis" \label{tab:main}) ///
 mtitles(`mtitles') drop(*.year *.admin_id) label coeflabels($coeflabel)  ///
 nobaselevels  order(`order') /// 
 addnotes("Entries in parentheses are standard errors clustered at facility ($i$).")
 
  
 ***************
 ***************

foreach n in 500 450 400 350 300 250 200 150 100 50 30 10 {
foreach i in 1 {
capture rename lb0_`n'_`i'1 lb0_`n'
capture rename ub0_`n'_`i'1 ub0_`n'
capture rename me0_`n'_`i'1 me0_`n'
capture rename lb1_`n'_`i'1 lb1_`n'
capture rename ub1_`n'_`i'1 ub1_`n'
capture rename me1_`n'_`i'1 me1_`n'
}
}

keep me0_* lb0_* ub0_* me1_* lb1_* ub1_*
gen id=_n
keep if id==1
reshape long me0_ lb0_ ub0_ me1_ lb1_ ub1_, i(id) j(d)

rename ub0_ ub0
rename lb0_ lb0
rename me0_ me0
rename ub1_ ub1
rename lb1_ lb1
rename me1_ me1
replace id = _n
reshape long me lb ub, i(id) j(foreign)

joinby d using battle_death.dta, unmatched(both) _merge(a)
drop a

*Correction
//me1_450_11	lb1_450_11	ub1_450_11
//-.2481623	-.3751713	-.1211532


replace lb = -.3751713 if d==450&foreign==1
replace ub = -.1211532 if d==450&foreign==1


graph twoway (rcap lb ub d if foreign==1, lpattern(shortdash) lc(red%60)) ///
(connected me d if foreign==1, mcolor(red%30) yline(0) msize(small) lpattern(solid) lc(red) ylabel(, gmax angle(horizontal))) ///
(bar battle_1 d, lw(vthin) fcolor(none) lc(gray) barwidth(10) yaxis(2)), /// 
xtitle("Distance from Mining Facilities (km)") xlabel(, labsize(medsmall) angle(20)) graphregion(fcolor(white)) ytitle("Average No. of Battles", axis(2)) ///
ytitle(" ", size(medium) margin(tiny)) ///
legend(on pos(6) col(3) order(3 "25<Death<100" )) xsize(15) ysize(10) name(g1_1, replace)

 
 ***************
 ***************
 
 *(7-3) 500>death>100
use data.dta, clear //49,776 obs.

foreach n in 500 450 400 350 300 250 200 150 100 50 30 10 {
foreach i in 2 {
local y battle_`n'_`i'
reg `y' i.foreign##i.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_`n' domestic_`n' csr_new i.year i.admin_id [pw=w_battle_`n'_`i'_a], cluster(rec_id)
est sto m2_`n'_`i' 

matrix out=r(table)'
matrix list out
scalar b_interv  = out["1.intervention", 1]
scalar se_interv = out["1.intervention", 2]

scalar b_interact  = out["1.foreign#1.intervention", 1]
scalar se_interact = out["1.foreign#1.intervention", 2]

matrix V = e(V)

scalar covar = V["1.intervention", "1.foreign#1.intervention"]

* marginal effect at foreign = 0
scalar me0 = b_interv
scalar se0 = se_interv
scalar ci_lo0 = me0 - 1.96 * se0
scalar ci_hi0 = me0 + 1.96 * se0

* marginal effect at foreign = 1
scalar me1 = b_interv + b_interact
scalar var1 = V["1.intervention", "1.intervention"] + V["1.foreign#1.intervention", "1.foreign#1.intervention"] + 2 * covar
scalar se1 = sqrt(var1)
scalar ci_lo1 = me1 - 1.96 * se1
scalar ci_hi1 = me1 + 1.96 * se1

capture drop lb0_`n'_`i'1
capture drop ub0_`n'_`i'1
capture drop me0_`n'_`i'1
capture drop lb1_`n'_`i'1
capture drop ub1_`n'_`i'1
capture drop me1_`n'_`i'1

matrix dydx_0=(me0)
matrix lb95_0=(ci_lo0)
matrix ub95_0=(ci_hi0)
matrix dydx_1=(me1)
matrix lb95_1=(ci_lo1)
matrix ub95_1=(ci_hi1)

svmat dydx_0, names(me0_`n'_`i')
svmat lb95_0, names(lb0_`n'_`i')
svmat ub95_0, names(ub0_`n'_`i')

svmat dydx_1, names(me1_`n'_`i')
svmat lb95_1, names(lb1_`n'_`i')
svmat ub95_1, names(ub1_`n'_`i')

}
}

est tab m2_500_2 m2_450_2 m2_400_2 m2_350_2 m2_300_2, ///
b(%9.3f) star(0.1 0.05 0.01) stats(N r2_a) drop(i.year i.admin_id) 

est tab m2_250_2 m2_200_2 m2_150_2 m2_100_2 m2_50_2 m2_30_2 m2_10_2,  ///
b(%9.3f) star(0.1 0.05 0.01) stats(N r2_a) drop(i.year i.admin_id) 

********
***************

global models m2_500_2 m2_450_2 m2_400_2 m2_350_2 m2_300_2 m2_250_2 m2_200_2 m2_150_2 m2_100_2 m2_50_2 m2_30_2 m2_10

global coeflabel 1.foreign "Foreign_{i,t}" 1.intervention "T_{i,t}" 1.foreign#1.intervention "Foreign_{i,t} $\times$ T_{i,t}" lprice "Logged Commodity Price_{c,t-1}" pricechange "\Delta Commodity Price_{c,t-1}" mean "Average Ruggedness_i" min "Minimum Ruggedness_i" max "Maximum Ruggedness_i" std "Std. Dev. Ruggedness_i" past5 "Past Battles within 5 Years_{j,t}" polity2 "POLITY_{j,t-1}" gdppc "GDPPC_{j,t-1}" gdppc2 "GDPPC_{j,t-1}^2" gdppc3 "GDPPC_{j,t-1}^3" log_aid "Logged Aid_{j, t-1}" reg_corruption "Corruption_{j, t-1}" i.facility_type "Facility Type_{i}" csr_new "CSR Score_{i,t}"


local order 1.foreign 1.intervention 1.foreign#1.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption facility_type foreign_* domestic_* csr_new 

local mtitles "500" "450" "400" "350" "300" "250" "200" "150" "100" "50" "30" "10"

esttab $models using "tab_g5.tex",   ///
 b(%9.3f) se(%9.3f)  scalars(r2_a) sfmt(%9.2f) ///
 compress notes nogaps nolines replace star(* 0.1 ** 0.05 *** 0.01) nonumbers ///
 title("Regression Analysis" \label{tab:main}) ///
 mtitles(`mtitles') drop(*.year *.admin_id) label coeflabels($coeflabel)  ///
 nobaselevels  order(`order') /// 
 addnotes("Entries in parentheses are standard errors clustered at facility ($i$).")
 
  ***************
 ***************

foreach n in 500 450 400 350 300 250 200 150 100 50 30 10 {
foreach i in 2 {
capture rename lb0_`n'_`i'1 lb0_`n'
capture rename ub0_`n'_`i'1 ub0_`n'
capture rename me0_`n'_`i'1 me0_`n'
capture rename lb1_`n'_`i'1 lb1_`n'
capture rename ub1_`n'_`i'1 ub1_`n'
capture rename me1_`n'_`i'1 me1_`n'
}
}

keep me0_* lb0_* ub0_* me1_* lb1_* ub1_*
gen id=_n
keep if id==1
reshape long me0_ lb0_ ub0_ me1_ lb1_ ub1_, i(id) j(d)

rename ub0_ ub0
rename lb0_ lb0
rename me0_ me0
rename ub1_ ub1
rename lb1_ lb1
rename me1_ me1
replace id = _n
reshape long me lb ub, i(id) j(foreign)

joinby d using battle_death.dta, unmatched(both) _merge(a)
drop a

*Correction
//me1_450_21	lb1_450_21	ub1_450_21
//-.0525751	-.0750116	-.0301386

replace lb = -.0750116 if d==450&foreign==1
replace ub = -.0301386 if d==450&foreign==1

graph twoway (rcap lb ub d if foreign==1, lpattern(shortdash) lc(red%60)) ///
(connected me d if foreign==1, mcolor(red%30) yline(0) msize(small) lpattern(solid) lc(red) ylabel(, gmax angle(horizontal))) ///
(bar battle_2 d, lw(vthin) fcolor(none) lc(gray) barwidth(10) yaxis(2)), /// 
xtitle("Distance from Mining Facilities (km)") xlabel(, labsize(medsmall) angle(20)) graphregion(fcolor(white)) ytitle("Average No. of Battles", axis(2)) ///
ytitle(" ", size(medium) margin(tiny)) ///
legend(on pos(6) col(3) order(3 "100<Death<500" )) xsize(15) ysize(10) name(g1_2, replace)

 ***************
 ***************


*(7-4) death>500
use data.dta, clear //49,776 obs.

foreach n in 500 450 400 350 300 250 200 150 100 50 30 10 {
foreach i in 3 {
local y battle_`n'_`i'
reg `y' i.foreign##i.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_`n' domestic_`n' csr_new i.year i.admin_id [pw=w_battle_`n'_`i'_a], cluster(rec_id)
est sto m2_`n'_`i' 

matrix out=r(table)'
matrix list out
scalar b_interv  = out["1.intervention", 1]
scalar se_interv = out["1.intervention", 2]

scalar b_interact  = out["1.foreign#1.intervention", 1]
scalar se_interact = out["1.foreign#1.intervention", 2]

matrix V = e(V)

scalar covar = V["1.intervention", "1.foreign#1.intervention"]

* marginal effect at foreign = 0
scalar me0 = b_interv
scalar se0 = se_interv
scalar ci_lo0 = me0 - 1.96 * se0
scalar ci_hi0 = me0 + 1.96 * se0

* marginal effect at foreign = 1
scalar me1 = b_interv + b_interact
scalar var1 = V["1.intervention", "1.intervention"] + V["1.foreign#1.intervention", "1.foreign#1.intervention"] + 2 * covar
scalar se1 = sqrt(var1)
scalar ci_lo1 = me1 - 1.96 * se1
scalar ci_hi1 = me1 + 1.96 * se1

capture drop lb0_`n'_`i'1
capture drop ub0_`n'_`i'1
capture drop me0_`n'_`i'1
capture drop lb1_`n'_`i'1
capture drop ub1_`n'_`i'1
capture drop me1_`n'_`i'1

matrix dydx_0=(me0)
matrix lb95_0=(ci_lo0)
matrix ub95_0=(ci_hi0)
matrix dydx_1=(me1)
matrix lb95_1=(ci_lo1)
matrix ub95_1=(ci_hi1)

svmat dydx_0, names(me0_`n'_`i')
svmat lb95_0, names(lb0_`n'_`i')
svmat ub95_0, names(ub0_`n'_`i')

svmat dydx_1, names(me1_`n'_`i')
svmat lb95_1, names(lb1_`n'_`i')
svmat ub95_1, names(ub1_`n'_`i')

}
}

est tab m2_500_3 m2_450_3 m2_400_3 m2_350_3 m2_300_3, ///
b(%9.3f) star(0.1 0.05 0.01) stats(N r2_a) drop(i.year i.admin_id) 

est tab m2_250_3 m2_200_3 m2_150_3 m2_100_3 m2_50_3 m2_30_3 m2_10_3,  ///
b(%9.3f) star(0.1 0.05 0.01) stats(N r2_a) drop(i.year i.admin_id) 

********
***************

global models m2_500_3 m2_450_3 m2_400_3 m2_350_3 m2_300_3 m2_250_3 m2_200_3 m2_150_3 m2_100_3 m2_50_3 m2_30_3 m2_10

global coeflabel 1.foreign "Foreign_{i,t}" 1.intervention "T_{i,t}" 1.foreign#1.intervention "Foreign_{i,t} $\times$ T_{i,t}" lprice "Logged Commodity Price_{c,t-1}" pricechange "\Delta Commodity Price_{c,t-1}" mean "Average Ruggedness_i" min "Minimum Ruggedness_i" max "Maximum Ruggedness_i" std "Std. Dev. Ruggedness_i" past5 "Past Battles within 5 Years_{j,t}" polity2 "POLITY_{j,t-1}" gdppc "GDPPC_{j,t-1}" gdppc2 "GDPPC_{j,t-1}^2" gdppc3 "GDPPC_{j,t-1}^3" log_aid "Logged Aid_{j, t-1}" reg_corruption "Corruption_{j, t-1}" i.facility_type "Facility Type_{i}" csr_new "CSR Score_{i,t}"


local order 1.foreign 1.intervention 1.foreign#1.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption facility_type foreign_* domestic_* csr_new 

local mtitles "500" "450" "400" "350" "300" "250" "200" "150" "100" "50" "30" "10"

esttab $models using "tab_g6.tex",   ///
 b(%9.3f) se(%9.3f)  scalars(r2_a) sfmt(%9.2f) ///
 compress notes nogaps nolines replace star(* 0.1 ** 0.05 *** 0.01) nonumbers ///
 title("Regression Analysis" \label{tab:main}) ///
 mtitles(`mtitles') drop(*.year *.admin_id) label coeflabels($coeflabel)  ///
 nobaselevels  order(`order') /// 
 addnotes("Entries in parentheses are standard errors clustered at facility ($i$).")
 
  ***************
 ***************

foreach n in 500 450 400 350 300 250 200 150 100 50 30 10 {
foreach i in 3 {
capture rename lb0_`n'_`i'1 lb0_`n'
capture rename ub0_`n'_`i'1 ub0_`n'
capture rename me0_`n'_`i'1 me0_`n'
capture rename lb1_`n'_`i'1 lb1_`n'
capture rename ub1_`n'_`i'1 ub1_`n'
capture rename me1_`n'_`i'1 me1_`n'
}
}

keep me0_* lb0_* ub0_* me1_* lb1_* ub1_*
gen id=_n
keep if id==1
reshape long me0_ lb0_ ub0_ me1_ lb1_ ub1_, i(id) j(d)

rename ub0_ ub0
rename lb0_ lb0
rename me0_ me0
rename ub1_ ub1
rename lb1_ lb1
rename me1_ me1
replace id = _n
reshape long me lb ub, i(id) j(foreign)

joinby d using battle_death.dta, unmatched(both) _merge(a)
drop a


*Correction
//me1_450_31	lb1_450_31	ub1_450_31
//-.0019248	-.003708	-.0001416


replace lb = -.003708 if d==450&foreign==1
replace ub = -.0001416 if d==450&foreign==1


graph twoway (rcap lb ub d if foreign==1, lpattern(shortdash) lc(red%60)) ///
(connected me d if foreign==1, mcolor(red%30) yline(0) msize(small) lpattern(solid) lc(red) ylabel(, gmax angle(horizontal))) ///
(bar battle_3 d, lw(vthin) fcolor(none) lc(gray) barwidth(10) yaxis(2)), /// 
xtitle("Distance from Mining Facilities (km)") xlabel(, labsize(medsmall) angle(20)) graphregion(fcolor(white)) ytitle("Average No. of Battles", axis(2)) ///
ytitle(" ", size(medium) margin(tiny)) ///
legend(on pos(6) col(3) order(3 "500<Death" )) xsize(15) ysize(10) name(g1_3, replace)

 ***************
 ***************
 *Figure G4
 gr combine g1_0 g1_1 g1_2 g1_3, col(2) xsize(20) ysize(10) scale(1) title("") graphregion(fcolor(white))
graph export figG4.png, width(2000) as(png) replace


*(8) Home countries' Mil expenditure (Table G7 and Figure 5)
use data.dta, clear //49,776 obs.

capture drop milex
gen milex = log(max_milex*1000+1)

local y battle_300
reg `y' i.intervention##c.milex lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m4_300

reg `y' i.intervention##c.milex lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c] if UnitedStates==0, cluster(rec_id)
est sto m4_300_2

est tab m4_300 m4_300_2, ///
b(%9.3f) star(0.1 0.05 0.01) stats(N r2_a) drop(i.year i.admin_id) 

********
***************
*(8-1) Table G7
global models m4_300 m4_300_2

global coeflabel milex "Mil. Exp._{i,t}" 1.intervention "T_{i,t}" milex#1.intervention "Foreign_{i,t} $\times$ T_{i,t}" lprice "Logged Commodity Price_{c,t-1}" pricechange "\Delta Commodity Price_{c,t-1}" mean "Average Ruggedness_i" min "Minimum Ruggedness_i" max "Maximum Ruggedness_i" std "Std. Dev. Ruggedness_i" past5 "Past Battles within 5 Years_{j,t}" polity2 "POLITY_{j,t-1}" gdppc "GDPPC_{j,t-1}" gdppc2 "GDPPC_{j,t-1}^2" gdppc3 "GDPPC_{j,t-1}^3" log_aid "Logged Aid_{j, t-1}" reg_corruption "Corruption_{j, t-1}" i.facility_type "Facility Type_{i}" foreign_300 "Foreign-owned Mines_{i,t}" domestic_300 "Domestic-owned Mines_{i,t}" csr_new "CSR Score_{i,t}"


local order milex 1.intervention milex#1.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption facility_type foreign_* domestic_* foreign_300 domestic_300 csr_new 

local mtitles "1" "2"

esttab $models using "tab_g7.tex",   ///
 b(%9.3f) se(%9.3f)  scalars(r2_a) sfmt(%9.2f) ///
 compress notes nogaps nolines replace star(* 0.1 ** 0.05 *** 0.01) nonumbers ///
 title("Regression Analysis" \label{tab:main}) ///
 mtitles(`mtitles') drop(*.year *.admin_id) label coeflabels($coeflabel)  ///
 nobaselevels  order(`order') /// 
 addnotes("Entries in parentheses are standard errors clustered at facility ($i$).")
 
 ***************
 ***************
*(8-2) Figure 5
capture drop me41
capture drop lb41
capture drop ub41

est restore m4_300
margins, dydx(intervention) at(milex=(18 (2) 28))
matrix out=r(table)'
matrix list out
matrix dydx=out[7..12,1]
matrix lb95=out[7..12,5]
matrix ub95=out[7..12,6]
svmat dydx, names(me4)
svmat lb95, names(lb4)
svmat ub95, names(ub4)

capture drop at
gen at = [(_n+8)*2]
replace at = . if at>28

graph twoway  (rline lb41 ub41 at, lpattern(dash) lc(navy%60)) ///
(hist milex if milex>0, yaxis(2) freq fcolor(none) lcolor(gray) ) ///
(connected me41 at, mcolor(red) yline(0) msize(zero) lpattern(solid) lc(navy) ylabel(, gmax angle(horizontal))), /// 
xtitle("Military Expenditure (British Pounds)") graphregion(fcolor(white)) ///
ylabel(, axis(2)) ytitle("Distribution of Military Expenditure", axis(2)) xlabel(17.727534 "50M" 18.420681 "100M" 20.030119 "500M" 20.723266 "1B" 22.332704 "5B" 23.025851 "10B" 24.635289 "50B" 25.328436 "100B" 26.937874 "500B" 27.631021 "1T", labsize(medsmall)) ///
legend(on col(1) pos(6) order(2 "Marginal Effect of Mining Operation")) name(g1, replace) 

est restore m4_300_2

capture drop me421
capture drop lb421
capture drop ub421

margins, dydx(intervention) at(milex=(18 (2) 26))
matrix out=r(table)'
matrix list out
matrix dydx=out[6..10,1]
matrix lb95=out[6..10,5]
matrix ub95=out[6..10,6]
svmat dydx, names(me42)
svmat lb95, names(lb42)
svmat ub95, names(ub42)

capture drop at
gen at = [(_n+8)*2]
replace at = . if at>26

graph twoway  (rline lb421 ub421 at, lpattern(dash) lc(navy%60)) ///
(hist milex if milex>0&milex<26, yaxis(2) freq fcolor(none) lcolor(gray) ) ///
(connected me421 at, mcolor(red) yline(0) msize(zero) lpattern(solid) lc(navy) ylabel(, gmax angle(horizontal))), /// 
xtitle("Military Expenditure (British Pounds)") graphregion(fcolor(white)) ///
ylabel(, axis(2)) ytitle("Distribution of Military Expenditure", axis(2)) xlabel(17.727534 "50M" 18.420681 "100M" 20.030119 "500M" 20.723266 "1B" 22.332704 "5B" 23.025851 "10B" 24.635289 "50B" 25.328436 "100B", labsize(medsmall)) ///
legend(on col(1) pos(6) order(2 "Marginal Effect of Mining Operation (Non-US Miners)")) name(g2, replace) 


gr combine g1 g2, xsize(20) ysize(10) scale(1) title("") graphregion(fcolor(white))
graph export fig5.png, width(2000) as(png) replace

																					
*****************
*****************



*(9) Alternative Explanations (Appendix F)
use data.dta, clear //49,776 obs.
collapse (mean)battle_300, by(facility_type)
rename battle_300 battle_fac
save battle_fac.dta, replace

*(9-1) Firm Size Control (Model F1 in Table F1)
use data.dta, clear //49,776 obs.
local y battle_300
reg `y' i.foreign##i.intervention max_mines lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m5_300

*(9-2)facility_type (Table F3 and Figure F2)

local y battle_300
reg `y' i.facility_type##i.intervention##i.foreign lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m5_300_fac

global models m5_300_fac
global coeflabel 1.foreign "Foreign_{i,t}" 1.intervention "T_{i,t}" 1.foreign#1.intervention "Foreign_{i,t} $\times$ T_{i,t}" lprice "Logged Commodity Price_{c,t-1}" pricechange "\Delta Commodity Price_{c,t-1}" mean "Average Ruggedness_i" min "Minimum Ruggedness_i" max "Maximum Ruggedness_i" std "Std. Dev. Ruggedness_i" past5 "Past Battles within 5 Years_{j,t}" polity2 "POLITY_{j,t-1}" gdppc "GDPPC_{j,t-1}" gdppc2 "GDPPC_{j,t-1}^2" gdppc3 "GDPPC_{j,t-1}^3" log_aid "Logged Aid_{j, t-1}" reg_corruption "Corruption_{j, t-1}" i.facility_type "Facility Type_{i}" foreign_300 "Foreign-owned Mines_{i,t}" domestic_300 "Domestic-owned Mines_{i,t}" csr_new "CSR Score_{i,t}"


local order 1.foreign 1.intervention 1.foreign#1.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption facility_type foreign_300 domestic_300 csr_new 

local mtitles "1" "2"

esttab $models using "tab_f3.tex",   ///
 b(%9.3f) se(%9.3f)  scalars(r2_a) sfmt(%9.2f) ///
 compress notes nogaps nolines replace star(* 0.1 ** 0.05 *** 0.01) nonumbers ///
 title("Regression Analysis" \label{tab:main}) ///
 mtitles(`mtitles') drop(*.year *.admin_id) label coeflabels($coeflabel)  ///
 nobaselevels  order(`order') /// 
 addnotes("Entries in parentheses are standard errors clustered at facility ($i$).")
 
 ****************
 ****************
 
capture drop me1
capture drop lb1
capture drop ub1

est restore m5_300_fac
margins, dydx(intervention) at(facility_type=(2 (1) 7) foreign=(1))
matrix out=r(table)'
matrix list out
matrix dydx=out[7..12,1]
matrix lb95=out[7..12,5]
matrix ub95=out[7..12,6]
svmat dydx, names(me)
svmat lb95, names(lb)
svmat ub95, names(ub)

capture drop at

gen at = _n+1
replace at = . if at>7

joinby facility_type using battle_fac.dta, unmatched(master) _merge(a)
tab a
drop a

graph twoway  (rcap lb1 ub1 at, lpattern(dash) lc(red%60)) ///
(hist facility_type if facility_type>1&foreign==1, barwidth(0.2) yaxis(2) disc freq fcolor(none) lw(vthin) lcolor(gray) ylabel(1000 "1K" 5000 "5K" 10000 "10K" 15000 "15K"20000 "20K", axis(2) gmax)) ///
(scatter me1 at, mcolor(red) yline(0) msize(1) lpattern(solid) lc(navy) ylabel(-10 (5) 5, gmax angle(horizontal))), xtitle("Foreign-owned") /// 
xlabel(1.5 " " 2 "Surface Extraction" 3 "Underground Extraction" 4 "Offshore Extraction" 5 "Processing Facility" 6 "Refining Facility" 7 "Smelting Facility" 7.2 " ", labsize(medsmall) angle(25)) ///
ylabel(, axis(2)) ytitle("Number of Facilities", axis(2) size(medium)) ytitle("Marginal Effect of Mining Operation", size(medium)) ///
legend(off) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) ///
lcolor(white) ifcolor(white) ilcolor(white)) name(g1, replace)

graph twoway (bar battle_fac facility_type, barwidth(0.2) fcolor(none) lc(gray)) ///
 (sc battle_300 facility_type, mcolor(navy%10) lw(none) yaxis(2)), ///
xtitle(" ")  /// 
xlabel(0.5 " " 1 "Artisanal Extraction" 2 "Surface Extraction" 3 "Underground Extraction" 4 "Offshore Extraction" 5 "Processing Facility" 6 "Refining Facility" 7 "Smelting Facility" 7.2 " ", labsize(medsmall) angle(25)) ///
 ytitle("Average Number of Battles", size(medium)) ytitle("Distribution of Battles within 300 km", axis(2) size(medium))  ///
legend(off) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) ///
lcolor(white) ifcolor(white) ilcolor(white)) name(g2, replace)


gr combine g1 g2, xsize(20) ysize(10) scale(1) title("") graphregion(fcolor(white))
graph export figF2.png, width(2000) as(png) replace

*(9-3)Interaction (Model F2 in Table F1)
keep if foreign==1 //12,160 obs.
tab time //balanced!
sum max_mines // 0 -- 85
sum max_mines if max_mines==0 //5,511 obs. out of 12,160

local y battle_300
reg `y' c.max_mines##i.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m5_300_int

*(9-4) Figure F1
est restore m5_300_int
margins, dydx(max_mines) at(intervention=(0 1)) plot(ciopts(lpattern(dash)) recast(scatter) ///
ytitle("", size(medium) margin(small)) yline(0)  ///
ylabel(, labsize(medsmall) angle(zero)) xtitle("Period", size(medium)) ///
xlabel(-0.1 " " 0 "Pre-operation" 1 "Post-operation" 1.1  " ", labsize(medsmall)) ///
title("") legend(on pos(6) order(2 "Marginal Effect of Firm Size") rows(2))  ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) ///
lcolor(white) ifcolor(white) ilcolor(white)) name(g1, replace))

capture drop me1
capture drop lb1
capture drop ub1

est restore m5_300_int
margins, dydx(intervention) at(max_mines=(0 (1) 85))
matrix out=r(table)'
matrix list out
matrix dydx=out[87..172,1]
matrix lb95=out[87..172,5]
matrix ub95=out[87..172,6]
svmat dydx, names(me)
svmat lb95, names(lb)
svmat ub95, names(ub)

capture drop at
gen at = [_n]
replace at = . if at>85

graph twoway  (rline lb1 ub1 at, lpattern(dash) lc(navy%60)) ///
(hist max_mines if time==0, yaxis(2) disc freq fcolor(none) lw(vthin) lcolor(gray) ) ///
(connected me1 at, mcolor(red) yline(0) msize(zero) lpattern(solid) lc(navy) ylabel(, gmax angle(horizontal))), /// 
xtitle("Firm Size") xlabel(0(5)85) graphregion(fcolor(white)) ///
ylabel(, axis(2)) ytitle("Number of Firms", axis(2) size(medium)) ytitle("Marginal Effect of Mining Operation", size(medium)) ///
legend(on pos(6) col(1) order(2 "Battles within 300 km") width(60)) name(g2, replace)


gr combine g2 g1, xsize(20) ysize(10) scale(1) title("") graphregion(fcolor(white))
graph export figF1.png, width(2000) as(png) replace

****************
****************
*(9-5) Table F1
global models m5_300 m5_300_int


global coeflabel 1.foreign "Foreign_{i,t}" 1.intervention "T_{i,t}" 1.foreign#1.intervention "Foreign_{i,t} $\times$ T_{i,t}" lprice "Logged Commodity Price_{c,t-1}" pricechange "\Delta Commodity Price_{c,t-1}" mean "Average Ruggedness_i" min "Minimum Ruggedness_i" max "Maximum Ruggedness_i" std "Std. Dev. Ruggedness_i" past5 "Past Battles within 5 Years_{j,t}" polity2 "POLITY_{j,t-1}" gdppc "GDPPC_{j,t-1}" gdppc2 "GDPPC_{j,t-1}^2" gdppc3 "GDPPC_{j,t-1}^3" log_aid "Logged Aid_{j, t-1}" reg_corruption "Corruption_{j, t-1}" i.facility_type "Facility Type_{i}" foreign_300 "Foreign-owned Mines_{i,t}" domestic_300 "Domestic-owned Mines_{i,t}" csr_new "CSR Score_{i,t}"


local order 1.foreign 1.intervention 1.foreign#1.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption facility_type foreign_300 domestic_300 csr_new 

local mtitles "1" "2"

esttab $models using "tab_f1.tex",   ///
 b(%9.3f) se(%9.3f)  scalars(r2_a) sfmt(%9.2f) ///
 compress notes nogaps nolines replace star(* 0.1 ** 0.05 *** 0.01) nonumbers ///
 title("Regression Analysis" \label{tab:main}) ///
 mtitles(`mtitles') drop(*.year *.admin_id) label coeflabels($coeflabel)  ///
 nobaselevels  order(`order') /// 
 addnotes("Entries in parentheses are standard errors clustered at facility ($i$).")
 
 ****************
 ****************

 
*(9-6) CSR (Model F4)
local y battle_300
reg `y' c.csr_new##i.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m5_300_csr

*Figure F3
est restore m5_300_csr
margins, dydx(csr_new) at(intervention=(0 1)) plot(ciopts(lpattern(dash)) recast(scatter) ///
ytitle("", size(medium) margin(small)) yline(0)  ///
ylabel(, labsize(medsmall) angle(zero)) xtitle("Period", size(medium)) ///
xlabel(-0.1 " " 0 "Pre-operation" 1 "Post-operation" 1.1  " ", labsize(medsmall)) ///
title("") legend(on pos(6) order(2 "Marginal Effect of CSR Score") rows(2))  ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) ///
lcolor(white) ifcolor(white) ilcolor(white)) name(g1, replace))

capture drop me1
capture drop lb1
capture drop ub1

est restore m5_300_csr
margins, dydx(intervention) at(csr_new=(30 (5) 60))
matrix out=r(table)'
matrix list out
matrix dydx=out[8..14,1]
matrix lb95=out[8..14,5]
matrix ub95=out[8..14,6]
svmat dydx, names(me)
svmat lb95, names(lb)
svmat ub95, names(ub)

capture drop at
gen at = [_n*5+29-4]
replace at = . if at>60

graph twoway  (rline lb1 ub1 at, lpattern(dash) lc(navy%60)) ///
(hist csr_new if time==0, yaxis(2) disc freq fcolor(none) lw(vthin) lcolor(gray) ) ///
(connected me1 at, mcolor(red) yline(0) msize(zero) lpattern(solid) lc(navy) ylabel(, gmax angle(horizontal))), /// 
xtitle("CSR Score") xlabel(30(5)60) graphregion(fcolor(white)) ///
ylabel(, axis(2)) ytitle("Number of Firms", axis(2) size(medium)) ytitle("Marginal Effect of Mining Operation", size(medium)) ///
legend(on pos(6) col(1) order(2 "Battles within 300 km") width(60)) name(g2, replace)


gr combine g2 g1, xsize(20) ysize(10) scale(1) title("") graphregion(fcolor(white))
graph export figF3.png, width(2000) as(png) replace


*(9-7) IIAs (Model F5)

local y battle_300
reg `y' c.iia##i.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m5_300_iia

*Figure F4
est restore m5_300_iia
margins, dydx(iia) at(intervention=(0 1)) plot(ciopts(lpattern(dash)) recast(scatter) ///
ytitle("", size(medium) margin(small)) yline(0)  ///
ylabel(, labsize(medsmall) angle(zero)) xtitle("Period", size(medium)) ///
xlabel(-0.1 " " 0 "Pre-operation" 1 "Post-operation" 1.1  " ", labsize(medsmall)) ///
title("") legend(on pos(6) order(2 "Marginal Effect of IIAs") rows(2))  ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) ///
lcolor(white) ifcolor(white) ilcolor(white)) name(g1, replace))
graph export figF4.png, width(2000) as(png) replace

*Table F4

global models m5_300_csr m5_300_iia

global coeflabel 1.foreign "Foreign_{i,t}" 1.intervention "T_{i,t}" 1.foreign#1.intervention "Foreign_{i,t} $\times$ T_{i,t}" lprice "Logged Commodity Price_{c,t-1}" pricechange "\Delta Commodity Price_{c,t-1}" mean "Average Ruggedness_i" min "Minimum Ruggedness_i" max "Maximum Ruggedness_i" std "Std. Dev. Ruggedness_i" past5 "Past Battles within 5 Years_{j,t}" polity2 "POLITY_{j,t-1}" gdppc "GDPPC_{j,t-1}" gdppc2 "GDPPC_{j,t-1}^2" gdppc3 "GDPPC_{j,t-1}^3" log_aid "Logged Aid_{j, t-1}" reg_corruption "Corruption_{j, t-1}" i.facility_type "Facility Type_{i}" foreign_300 "Foreign-owned Mines_{i,t}" domestic_300 "Domestic-owned Mines_{i,t}" csr_new "CSR Score_{i,t}" iia "IIAs"


local order csr_new iia 1.intervention csr_new#1.intervention iia#1.intervention  lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption facility_type foreign_300 domestic_300  

local mtitles "1" "2"

esttab $models using "tab_f4.tex",   ///
 b(%9.3f) se(%9.3f)  scalars(r2_a) sfmt(%9.2f) ///
 compress notes nogaps nolines replace star(* 0.1 ** 0.05 *** 0.01) nonumbers ///
 title("Regression Analysis" \label{tab:main}) ///
 mtitles(`mtitles') drop(*.year *.admin_id) label coeflabels($coeflabel)  ///
 nobaselevels  order(`order') /// 
 addnotes("Entries in parentheses are standard errors clustered at facility ($i$).")
 
****************
****************

*(9-8) Reverse Causation
use data.dta, clear //49,776 obs.

*Timing Dummies (Figure F5 and Table F5)
replace time = time+6
label define timing 1 "T-5" 2 "T-4" 3 "T-3" 4 "T-2" 5 "T-1" 6 "T" 7 "T+1" 8 "T+2" 
label values time timing

reg battle_300 c.foreign##i.time lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id, cluster(rec_id)
est sto m6_time_dummy

*Figure F5
est restore m6_time_dummy

reg battle_300 c.foreign##i.time lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m6_2_time_dummy

est restore m6_2_time_dummy

margins, dydx(foreign) at(time = (1(1)8)) plot(recast(scatter) ///
ciopts(lpattern(dash)) ytitle("Marginal Effect of Foreign Ownership") ytitle(, size(medium) ///
margin(small)) yline(0) ylabel(, labsize(medsmall) angle(zero)) xtitle(Ownership) ///
xtitle("Timing", size(medium)) xlabel(, labsize(medsmall)) ///
title("Post-matching Sample") legend(on pos(6) order(1 "95% Confidence Interval" 2 "Battles within 300 km") rows(2)) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) ///
lcolor(white) ifcolor(white) ilcolor(white)) name(g2, replace))

graph export figF5.png, width(2000) as(png) replace

est tab m6_time_dummy m6_2_time_dummy , ///
b(%9.5f) star(0.1 0.05 0.01) stats(N r2_a) drop(i.year i.admin_id) 


*Table F5

global models m6_time_dummy m6_2_time_dummy 

global coeflabel foreign "Foreign_{i,t}" intervention "T_{i,t}" 1.foreign#1.intervention "Foreign_{i,t} $\times$ T_{i,t}" lprice "Logged Commodity Price_{c,t-1}" pricechange "\Delta Commodity Price_{c,t-1}" mean "Average Ruggedness_i" min "Minimum Ruggedness_i" max "Maximum Ruggedness_i" std "Std. Dev. Ruggedness_i" past5 "Past Battles within 5 Years_{j,t}" polity2 "POLITY_{j,t-1}" gdppc "GDPPC_{j,t-1}" gdppc2 "GDPPC_{j,t-1}^2" gdppc3 "GDPPC_{j,t-1}^3" log_aid "Logged Aid_{j, t-1}" reg_corruption "Corruption_{j, t-1}" i.facility_type "Facility Type_{i}" foreign_300 "Foreign-owned Mines_{i,t}" domestic_300 "Domestic-owned Mines_{i,t}" csr_new "CSR Score_{i,t}"


local order foreign lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption facility_type foreign_300 domestic_300 csr_new 

local mtitles "1" "2"

esttab $models using "tab_f5.tex",   ///
 b(%9.3f) se(%9.3f)  scalars(r2_a) sfmt(%9.2f) ///
 compress notes nogaps nolines replace star(* 0.1 ** 0.05 *** 0.01) nonumbers ///
 title("Regression Analysis" \label{tab:main}) ///
 mtitles(`mtitles') drop(*.year *.admin_id) label coeflabels($coeflabel)  ///
 nobaselevels  order(`order') /// 
 addnotes("Entries in parentheses are standard errors clustered at facility ($i$).")
 
****************
****************

*Reverse Regression with Lags and Leads (Table F6)


xtset rec_id year
reg foreign c.l3.battle_300 lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m0_revw

reg foreign c.l2.battle_300 lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m1_revw

reg foreign c.l1.battle_300 lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m2_revw

reg foreign c.battle_300 lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m3_revw

reg foreign c.f1.battle_300 lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m4_revw

reg foreign c.f2.battle_300 lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m5_revw

esttab m0_revw m1_revw m2_revw m3_revw m4_revw m5_revw using "tab_f6.tex",   ///
 b(%9.5f) se(%9.5f)  scalars(r2_a) sfmt(%9.2f) ///
 compress notes nogaps nolines replace star(* 0.1 ** 0.05 *** 0.01) nonumbers ///
 title("Regression Analysis" \label{tab:main}) ///
drop(*.year *.admin_id) nobaselevels /// 
 addnotes("Entries in parentheses are standard errors clustered at facility ($i$).")
 
****************
**************** 
 
 *(10) Commodity (Figure G3)
use data.dta, clear  

reg  battle_300 c.foreign##i.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
capture drop yhat2
predict yhat2, xb 

capture drop mean_lprice mean_bt_300
bysort commodity: egen mean_lprice=mean(lprice) 
bysort commodity: egen mean_bt_300=mean(yhat2)
keep commodity mean_lprice mean_bt_300
duplicates drop
twoway (scatter mean_lprice mean_bt_300 if commodity!="Cement"&commodity!="Copper"& ///
commodity!="Coal"&commodity!="Crude Oil"&commodity!="Diamond"& ///
commodity!="Gold"&commodity!="Iron and steel"&commodity!="Natural Gas"&commodity!="Platinum" ///
, mc(cranberry%50) mlabsize(vsmall) mlabc(cranberry%70) mlabel(commodity) mlabposition(1) mlabangle(stdarrow)) ///
(scatter mean_lprice mean_bt_300 if commodity=="Cement"|commodity=="Copper"| ///
commodity=="Coal"|commodity=="Crude Oil"|commodity=="Diamond"| ///
commodity=="Gold"|commodity=="Iron and steel"|commodity=="Natural Gas"|commodity=="Platinum" ///
, mc(navy) mlabc(navy) mlabsize(vsmall) mlabel(commodity) mlabposition(3) mlabangle(stdarrow)), ///
ytitle(Average Logged Unit Price) ylabel(1(2)19) xtitle(No. of Battles within 300 km, size(small)) xsize(15) ysize(20) ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) legend(off) name(com, replace) 

graph hbar (mean) mean_lprice, over(commodity, label(labsize(vsmall) angle(25))) ///
ytitle(Logged Unit Price, size(small)) xsize(10) ysize(20) graphregion(fcolor(white) ///
lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) ///
lcolor(white) ifcolor(white) ilcolor(white)) name(com2, replace) 

gr combine com com2, ///
xsize(15) ysize(20) scale(1) title("") graphregion(fcolor(white))
graph export figG3.png, width(2000) as(png) replace


*(11) Testing with other measures (Table G8)
*(7-1) V-dem, past5, gdp, ruggedness
use data.dta, clear  

reg  battle_300 c.foreign##i.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m7a

reg  battle_300 c.foreign##i.intervention lprice pricechange mean min max std past5 v2x_polyarchy gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m7b

reg  battle_300 c.foreign##i.intervention lprice pricechange mean min max std p_battle_300 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m7c

reg  battle_300 c.foreign##i.intervention lprice pricechange mean min max std p_battle_300 v2x_polyarchy gdppc gdppc2 gdppc3 log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m7d

 
 reg  battle_300 c.foreign##i.intervention lprice pricechange mean past5 polity2 gdppc log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m7e

reg  battle_300 c.foreign##i.intervention lprice pricechange mean past5 v2x_polyarchy gdppc log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m7f

reg  battle_300 c.foreign##i.intervention lprice pricechange mean p_battle_300 polity2 gdppc log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m7g

reg  battle_300 c.foreign##i.intervention lprice pricechange mean p_battle_300 v2x_polyarchy gdppc log_aid reg_corruption i.facility_type foreign_300 domestic_300 csr_new i.year i.admin_id [pw=w_battle_300_c], cluster(rec_id)
est sto m7h

estimates table m7a m7b m7c m7d ///
 , b(%9.3f) star(0.1 0.05 0.01) stats(N r2_a) drop(i.year i.admin_id)

estimates table m7e m7f m7g m7h ///
 , b(%9.3f) star(0.1 0.05 0.01) stats(N r2_a) drop(i.year i.admin_id)


****************
****************

global models m7a m7b m7c m7d m7e m7f m7g m7h
global coeflabel 1.foreign "Foreign_{i,t}" 1.intervention "T_{i,t}" 1.foreign#1.intervention "Foreign_{i,t} $\times$ T_{i,t}" lprice "Logged Commodity Price_{c,t-1}" pricechange "\Delta Commodity Price_{c,t-1}" mean "Average Ruggedness_i" min "Minimum Ruggedness_i" max "Maximum Ruggedness_i" std "Std. Dev. Ruggedness_i" past5 "Past Battles within 5 Years_{j,t}" polity2 "POLITY_{j,t-1}" gdppc "GDPPC_{j,t-1}" gdppc2 "GDPPC_{j,t-1}^2" gdppc3 "GDPPC_{j,t-1}^3" log_aid "Logged Aid_{j, t-1}" reg_corruption "Corruption_{j, t-1}" i.facility_type "Facility Type_{i}" foreign_300 "Foreign-owned Mines_{i,t}" domestic_300 "Domestic-owned Mines_{i,t}" csr_new "CSR Score_{i,t}"


local order foreign 1.intervention 1.foreign#1.intervention lprice pricechange mean min max std past5 polity2 gdppc gdppc2 gdppc3 log_aid reg_corruption facility_type foreign_300 domestic_300 csr_new 

local mtitles "a" "b" "c" "d" "e" "f" "g"

esttab $models using "tab_g8.tex",   ///
 b(%9.3f) se(%9.3f)  scalars(r2_a) sfmt(%9.2f) ///
 compress notes nogaps nolines replace star(* 0.1 ** 0.05 *** 0.01) nonumbers ///
 title("Regression Analysis" \label{tab:main}) ///
drop(*.year *.admin_id) nobaselevels /// 
 addnotes("Entries in parentheses are standard errors clustered at facility ($i$).")
 
****************
**************** 
 
capture log close




/*
 
